﻿Imports BUS
Imports DAO
Imports DTO
Public Class FormQuanLyThamSo

    Private Sub FormQuanLyThamSo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadThamSo()
        ButtonCapNhat.Enabled = False
    End Sub

#Region "Load tham số"
    Private Sub LoadThamSo()
        Dim tb As DataTable = ThamSo_BUS.LoadThamSo()
        dgvQuyDinh.DataSource = tb
        dgvQuyDinh.Columns(0).HeaderText = "Số lượng nhập tối thiểu"
        dgvQuyDinh.Columns(1).HeaderText = "Số lượng tồn tối thiểu"
        dgvQuyDinh.Columns(2).HeaderText = "Số nợ tối đa"
        dgvQuyDinh.Columns(3).HeaderText = "Số lượng tồn sau bán"
        dgvQuyDinh.ClearSelection()
    End Sub
#End Region

    Private Sub dgvQuyDinh_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvQuyDinh.CellClick
        TextBoxGiaTri.Text = dgvQuyDinh.CurrentCell.Value.ToString()
        ButtonCapNhat.Enabled = True
    End Sub

#Region "Xử lý nút cập nhật tham số"
    Private Sub ButtonCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCapNhat.Click
        Try
            Dim gt As Integer = Integer.Parse(TextBoxGiaTri.Text)
            Dim index As Integer = dgvQuyDinh.CurrentCell.ColumnIndex
            Dim tenthamso As String = dgvQuyDinh.Columns(index).Name
            ThamSo_BUS.SuaThamSo(tenthamso, gt)
            LoadThamSo()
            TextBoxGiaTri.Clear()
            ButtonCapNhat.Enabled = False
            MessageBox.Show("Sửa thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show("Sửa thất bại")
        End Try
    End Sub
#End Region

    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub


#Region "Đổi màu các button"
    Public Sub MauMoi(ByVal btn As Button, ByVal flag As Integer)

        If flag = 1 Then
            btn.BackColor = Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer))
        Else
            btn.BackColor = Color.Gray
        End If
    End Sub
#End Region


#Region " Gọi hàm đổi màu"
    Private Sub ButtonCapNhat_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCapNhat.EnabledChanged
        If ButtonCapNhat.Enabled = True Then
            MauMoi(ButtonCapNhat, 1)
        Else
            MauMoi(ButtonCapNhat, 2)
        End If
    End Sub
#End Region

End Class